package hoyo.zqy.org.controller;


import hoyo.zqy.basic.annotation.PreAuthorize;
import hoyo.zqy.basic.util.AjaxResult;
import hoyo.zqy.basic.util.PageList;
import hoyo.zqy.log.annotation.LogAnnotation;
import hoyo.zqy.org.domain.Department;
import hoyo.zqy.org.query.DepartmentQuery;
import hoyo.zqy.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;


    /**
     * PathVariable("id") Long id从路径上获取id的值赋值给long id
     * @param id
     * @return
     */
    //Get-http://lockhost//department/id
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "查询单个部门",sn = "department:getOne")
    public Department loadById(@PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    /**
     * 查询所有
     * @return
     */
    //Get-http://lockhost//department/
    @GetMapping
    @ApiOperation(value = "查询全部" )
    @PreAuthorize(name = "查询所有部门",sn = "department:getAll")
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    //delete//lockhost//department/id
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @LogAnnotation(module="部门",operater="删除部门")
    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);

            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"失败了,正在殴打程序猿中");
        }
    }


    //Put//lockhost//department
    @PutMapping
    @ApiOperation(value = "新增和修改" )
    @LogAnnotation(module="部门",operater="新增/修改部门")
    @PreAuthorize(name = "添加和修改",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"失败了,正在殴打程序猿中");
        }
    }


    //批量删除的接口
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    @LogAnnotation(module="部门",operater="批量删除部门")
    @PreAuthorize(name = "部门批量删除",sn = "department:patchDel")
    public AjaxResult deleBatch(@RequestBody Long[] ids) {
        try {
            System.out.println(Arrays.toString(ids));
            departmentService.deleBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            return new AjaxResult(false,"失败了,正在殴打程序猿中");
        }
    }

    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    @PreAuthorize(name = "部门树",sn = "department:deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
